import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _forEach from "lodash/forEach";
import _isNumber from "lodash/isNumber";
import _every from "lodash/every";
import _slice from "lodash/slice";
import _concat from "lodash/concat";
import _cloneDeep from "lodash/cloneDeep";
import _isFunction from "lodash/isFunction";

/**
 * CellControl
 * @date 2018/10/10
 */
import React from 'react';
import { Menu, Dropdown, Icon, Button, Modal, Popconfirm } from 'antd';
import notification from 'utils/notification';
import intl from 'utils/intl';
import styles from "../../index.less?modules";
import { emptyField, emptyFieldType } from "../../../config";
var menuBtnStyle = {
  padding: 2
};
var menuItemKey = {
  appendRow: 'appendRow',
  appendCol: 'appendCol',
  removeRow: 'removeRow',
  removeCol: 'removeCol',
  mergeRight: 'mergeRight',
  cancelMergeRight: 'cancelMergeRight'
};

var CellControl = /*#__PURE__*/function (_React$Component) {
  _inherits(CellControl, _React$Component);

  var _super = _createSuper(CellControl);

  function CellControl(props) {
    var _this;

    _classCallCheck(this, CellControl);

    _this = _super.call(this, props);
    _this.menuRef = React.createRef();
    _this.handleRemoveTrigger = _this.handleRemoveTrigger.bind(_assertThisInitialized(_this));
    _this.handleFieldMenuClick = _this.handleFieldMenuClick.bind(_assertThisInitialized(_this));
    _this.handleRemoveRow = _this.handleRemoveRow.bind(_assertThisInitialized(_this));
    _this.handleRemoveCol = _this.handleRemoveCol.bind(_assertThisInitialized(_this));
    _this.handleRefresh = _this.handleRefresh.bind(_assertThisInitialized(_this));
    _this.handleCleanWillRemovePositionX = _this.handleCleanWillRemovePositionX.bind(_assertThisInitialized(_this));
    _this.handleCleanWillRemovePositionY = _this.handleCleanWillRemovePositionY.bind(_assertThisInitialized(_this));
    return _this;
  }

  _createClass(CellControl, [{
    key: "render",
    value: function render() {
      var _this2 = this;

      var _this$props = this.props,
          children = _this$props.children,
          menuItemProps = _this$props.menuItemProps,
          component = _this$props.component,
          currentEditField = _this$props.currentEditField,
          willRemovePositionY = _this$props.willRemovePositionY,
          willRemovePositionX = _this$props.willRemovePositionX,
          positionY = _this$props.positionY,
          positionX = _this$props.positionX,
          positionRightX = _this$props.positionRightX,
          isOverAndCanDrop = _this$props.isOverAndCanDrop;
      var cellControlClassName = [styles['cell-control']];
      var isCurrentEditField = currentEditField === component;
      var isNoEmptyCell = component.componentType !== emptyFieldType;
      var isWillRemoveCell = false;

      if (positionY === willRemovePositionY) {
        // 当前字段在将要删除的行里面
        isWillRemoveCell = true;
      } else if (willRemovePositionX >= positionX && willRemovePositionX < positionRightX) {
        // 当前字段在将要删除的列里面
        isWillRemoveCell = true;
      }

      if (isCurrentEditField) {
        cellControlClassName.push(styles['cell-control-active']);
      }

      if (!isNoEmptyCell) {
        cellControlClassName.push(styles['cell-control-placeholder']);
      }

      if (isWillRemoveCell) {
        // if(component.colspan && isNumber(component.colspan)){
        //   cellControlClassName.push(styles['cell-control-will-remove-colspan']);
        // }else{
        cellControlClassName.push(styles['cell-control-will-remove']); // }
      }

      if (isOverAndCanDrop) {
        cellControlClassName.push(styles['cell-control-over']);
      }

      return /*#__PURE__*/React.createElement("div", {
        className: cellControlClassName.join(' ')
      }, /*#__PURE__*/React.createElement("div", {
        className: styles['cell-control-component']
      }, children), isNoEmptyCell && /*#__PURE__*/React.createElement("i", {
        className: styles['cell-control-remove']
      }, /*#__PURE__*/React.createElement(Popconfirm, {
        onConfirm: this.handleRemoveTrigger,
        title: intl.get('cpfm.ui.message.field.removeTitle').d('是否确认删除')
      }, /*#__PURE__*/React.createElement("i", {
        className: "anticon"
      }))), /*#__PURE__*/React.createElement("div", {
        className: styles['cell-control-menu'],
        ref: this.menuRef
      }, /*#__PURE__*/React.createElement(Dropdown, {
        getPopupContainer: function getPopupContainer() {
          return _this2.menuRef.current || document.body;
        },
        overlay: /*#__PURE__*/React.createElement(Menu, {
          onClick: this.handleFieldMenuClick
        }, /*#__PURE__*/React.createElement(Menu.Item, {
          key: menuItemKey.appendRow,
          disabled: !menuItemProps.canInsertBottom
        }, /*#__PURE__*/React.createElement("i", {
          className: "menu-icon menu-icon-append-row"
        }), intl.get('cpfm.codeRule.model.codeRule.addRow').d('向下新增一行')), /*#__PURE__*/React.createElement(Menu.Item, {
          key: menuItemKey.appendCol,
          disabled: !menuItemProps.canInsertRight
        }, /*#__PURE__*/React.createElement("i", {
          className: "menu-icon menu-icon-append-col"
        }), intl.get('cpfm.codeRule.model.codeRule.addColumn').d('向下新增一列')), /*#__PURE__*/React.createElement(Menu.Divider, null), /*#__PURE__*/React.createElement(Menu.Item, {
          key: menuItemKey.mergeRight,
          disabled: !menuItemProps.canMergeRight
        }, /*#__PURE__*/React.createElement("i", {
          className: "menu-icon menu-icon-merge-right"
        }), intl.get('cpfm.codeRule.model.codeRule.combineRight').d('向右合并')), /*#__PURE__*/React.createElement(Menu.Item, {
          key: menuItemKey.cancelMergeRight,
          disabled: !menuItemProps.canCancelMergeRight
        }, /*#__PURE__*/React.createElement("i", {
          className: "menu-icon menu-icon-cancel-merge-right"
        }), intl.get('cpfm.codeRule.model.codeRule.cancelCombine').d('取消合并')), /*#__PURE__*/React.createElement(Menu.Divider, null), /*#__PURE__*/React.createElement(Menu.Item, {
          key: menuItemKey.removeRow,
          disabled: !menuItemProps.canRemoveRow
        }, /*#__PURE__*/React.createElement("i", {
          className: "menu-icon menu-icon-remove-row"
        }), intl.get('cpfm.codeRule.model.codeRule.deleteRow').d('删除行')), /*#__PURE__*/React.createElement(Menu.Item, {
          key: menuItemKey.removeCol,
          disabled: !menuItemProps.canRemoveCol
        }, /*#__PURE__*/React.createElement("i", {
          className: "menu-icon menu-icon-remove-col"
        }), intl.get('cpfm.codeRule.model.codeRule.deleteCol').d('删除列')))
      }, /*#__PURE__*/React.createElement(Button, {
        style: menuBtnStyle
      }, /*#__PURE__*/React.createElement(Icon, {
        type: "down"
      })))));
    }
  }, {
    key: "handleRemoveTrigger",
    value: function handleRemoveTrigger(e) {
      e.stopPropagation();
      var _this$props2 = this.props,
          component = _this$props2.component,
          pComponent = _this$props2.pComponent,
          fieldOptions = _this$props2.fieldOptions,
          onRemoveField = _this$props2.onRemoveField;

      if (_isFunction(onRemoveField)) {
        onRemoveField(pComponent, component, fieldOptions);
      }
    }
  }, {
    key: "handleFieldMenuClick",
    value: function handleFieldMenuClick(_ref) {
      var key = _ref.key;

      switch (key) {
        case menuItemKey.appendRow:
          this.handleAppendRow();
          break;

        case menuItemKey.appendCol:
          this.handleAppendCol();
          break;

        case menuItemKey.removeRow:
          this.handleRemoveRowTrigger();
          break;

        case menuItemKey.removeCol:
          this.handleRemoveColTrigger();
          break;

        case menuItemKey.mergeRight:
          this.handleMergeRight();
          break;

        case menuItemKey.cancelMergeRight:
          this.handleCancelMergeRight();
          break;

        default:
          break;
      }
    }
    /**
     * 新增行
     */

  }, {
    key: "handleAppendRow",
    value: function handleAppendRow() {
      var _this$props3 = this.props,
          colCount = _this$props3.colCount,
          rowIndex = _this$props3.rowIndex,
          pComponent = _this$props3.pComponent;
      var newRow = [];

      for (var index = 0; index < colCount; index += 1) {
        newRow.push(_cloneDeep(emptyField));
      }

      var sliceIndex = rowIndex + 1;
      pComponent.fields = _concat([], _slice(pComponent.fields, 0, sliceIndex), [newRow], _slice(pComponent.fields, sliceIndex));
      this.handleRefresh();
    }
    /**
     * 删除行 触发方法
     */

  }, {
    key: "handleRemoveRowTrigger",
    value: function handleRemoveRowTrigger() {
      var _this$props4 = this.props,
          rowCount = _this$props4.rowCount,
          pComponent = _this$props4.pComponent,
          positionY = _this$props4.positionY,
          rowIndex = _this$props4.rowIndex;

      if (rowCount < 2) {
        notification.warning({
          message: intl.get('cpfm.codeRule.model.codeRule.canNotDelLastRow').d('不能删除最后一行')
        });
        return;
      }

      if (_every(pComponent.fields[rowIndex], function (field) {
        return field.componentType === emptyFieldType;
      })) {
        this.handleRemoveRow();
      } else {
        this.handleUpdateWillRemovePositionY(positionY);
        Modal.confirm({
          content: intl.get('cpfm.codeRule.model.codeRule.isDelCurRow').d('是否删除当前行'),
          onOk: this.handleRemoveRow,
          onCancel: this.handleCleanWillRemovePositionY
        });
      }
    }
    /**
     * todo 要将 site 字段放到 removeSiteFields 中
     * 删除行 实际方法
     */

  }, {
    key: "handleRemoveRow",
    value: function handleRemoveRow() {
      var _this$props5 = this.props,
          rowIndex = _this$props5.rowIndex,
          pComponent = _this$props5.pComponent;
      pComponent.fields = _concat([], _slice(pComponent.fields, 0, rowIndex), _slice(pComponent.fields, rowIndex + 1)); // // todo 租户级代码
      // const removeFields = pComponent.fields[rowIndex];
      // for (let index = 0; index < removeFields.length; index += 1) {
      //   const removeField = removeFields[index];
      //   if (removeField.siteFlag === 1) {
      //     removeField.visiableFlag = 0;
      //     pComponent.removeSiteFields.push(removeField);
      //   }
      // }

      this.handleCleanWillRemovePositionY();
      this.handleRefresh();
    }
    /**
     * 新增列
     */

  }, {
    key: "handleAppendCol",
    value: function handleAppendCol() {
      var _this$props6 = this.props,
          pComponent = _this$props6.pComponent,
          rowIndex = _this$props6.rowIndex,
          colIndex = _this$props6.colIndex; // 先找到当前字段的位置所占位置的最后

      var currentRow = pComponent.fields[rowIndex];
      var positionX = 0;

      for (var index = 0; index <= colIndex; index += 1) {
        var f = currentRow[index];

        if (f.colspan && _isNumber(f.colspan)) {
          positionX += f.colspan;
        } else {
          positionX += 1;
        }
      } // 得到当前字段的位置


      _forEach(pComponent.fields, function (fArr, rIndex) {
        var endPositionX = 0;

        _forEach(fArr, function (f, cIndex) {
          var isColspan = f.colspan && _isNumber(f.colspan);

          if (isColspan) {
            endPositionX += f.colspan;
          } else {
            endPositionX += 1;
          }

          if (endPositionX > positionX) {
            // 改变 colspan
            // eslint-disable-next-line
            f.colspan += 1;
            return false;
          } else if (endPositionX === positionX) {
            // 增加新的 emptyField
            var sliceIndex = cIndex + 1;
            pComponent.fields[rIndex] = _concat([], _slice(fArr, 0, sliceIndex), [_cloneDeep(emptyField)], _slice(fArr, sliceIndex));
            return false;
          }
        });
      });

      this.handleRefresh();
    }
    /**
     * 删除列 触发方法
     */

  }, {
    key: "handleRemoveColTrigger",
    value: function handleRemoveColTrigger() {
      var _this$props7 = this.props,
          colCount = _this$props7.colCount,
          positionRightX = _this$props7.positionRightX;

      if (colCount < 2) {
        notification.warning({
          message: intl.get('cpfm.codeRule.model.codeRule.canNotDelLastCol').d('不能删除最后一列')
        });
        return;
      }

      this.handleUpdateWillRemovePositionX(positionRightX - 1);
      Modal.confirm({
        content: intl.get('cpfm.codeRule.model.codeRule.isDelCurCol').d('是否删除当前列'),
        onOk: this.handleRemoveCol,
        onCancel: this.handleCleanWillRemovePositionX
      });
    }
    /**
     * todo 要将 site 字段放到 removeSiteFields 中
     * 删除列 实际方法
     */

  }, {
    key: "handleRemoveCol",
    value: function handleRemoveCol() {
      var _this$props8 = this.props,
          pComponent = _this$props8.pComponent,
          rowIndex = _this$props8.rowIndex,
          colIndex = _this$props8.colIndex; // 找到要删除的位置

      var currentRow = pComponent.fields[rowIndex];
      var positionX = 0;

      for (var index = 0; index <= colIndex; index += 1) {
        var f = currentRow[index];

        if (f.colspan && _isNumber(f.colspan)) {
          positionX += f.colspan;
        } else {
          positionX += 1;
        }
      }

      _forEach(pComponent.fields, function (fArr, rIndex) {
        var endPositionX = 0;

        _forEach(fArr, function (f, cIndex) {
          var isColspan = f.colspan && _isNumber(f.colspan);

          if (isColspan) {
            endPositionX += f.colspan;
          } else {
            endPositionX += 1;
          }

          if (endPositionX >= positionX && isColspan) {
            // colspan -1
            if (f.colspan === 2) {
              // eslint-disable-next-line
              delete f.colspan;
            } else {
              // eslint-disable-next-line
              f.colspan -= 1;
            }

            return false;
          } else if (endPositionX === positionX) {
            // const removeField = fArr[cIndex];
            // // todo 租户级的代码
            // if (removeField.siteFlag === 1) {
            //   removeField.visiableFlag = 0;
            //   pComponent.removeSiteFields.push(removeField);
            // }
            pComponent.fields[rIndex] = _concat([], _slice(fArr, 0, cIndex), _slice(fArr, cIndex + 1));
            return false;
          }
        });
      });

      this.handleCleanWillRemovePositionX();
      this.handleRefresh();
    }
    /**
     * 更新 要删除的下标
     */

  }, {
    key: "handleUpdateWillRemovePositionY",
    value: function handleUpdateWillRemovePositionY() {
      var willRemovePositionY = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : -1;
      var onUpdateWillRemovePositionY = this.props.onUpdateWillRemovePositionY;

      if (_isFunction(onUpdateWillRemovePositionY)) {
        onUpdateWillRemovePositionY(willRemovePositionY);
      }
    }
    /**
     * 更新 要删除的下标
     */

  }, {
    key: "handleUpdateWillRemovePositionX",
    value: function handleUpdateWillRemovePositionX() {
      var willRemovePositionX = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : -1;
      var onUpdateWillRemovePositionX = this.props.onUpdateWillRemovePositionX;

      if (_isFunction(onUpdateWillRemovePositionX)) {
        onUpdateWillRemovePositionX(willRemovePositionX);
      }
    }
    /**
     * 更新 要删除的下标
     */

  }, {
    key: "handleCleanWillRemovePositionY",
    value: function handleCleanWillRemovePositionY() {
      var onUpdateWillRemovePositionY = this.props.onUpdateWillRemovePositionY;

      if (_isFunction(onUpdateWillRemovePositionY)) {
        onUpdateWillRemovePositionY(-1);
      }
    }
    /**
     * 更新 要删除的下标
     */

  }, {
    key: "handleCleanWillRemovePositionX",
    value: function handleCleanWillRemovePositionX() {
      var onUpdateWillRemovePositionX = this.props.onUpdateWillRemovePositionX;

      if (_isFunction(onUpdateWillRemovePositionX)) {
        onUpdateWillRemovePositionX(-1);
      }
    }
    /**
     * 合并右侧空
     */

  }, {
    key: "handleMergeRight",
    value: function handleMergeRight() {
      var _this$props9 = this.props,
          rowIndex = _this$props9.rowIndex,
          colIndex = _this$props9.colIndex,
          pComponent = _this$props9.pComponent;
      var _pComponent$fields = pComponent.fields,
          fields = _pComponent$fields === void 0 ? [] : _pComponent$fields;
      var currentRow = fields[rowIndex];

      if (!(currentRow[colIndex + 1] && currentRow[colIndex + 1].componentType === emptyFieldType)) {
        notification.warning({
          message: intl.get('cpfm.codeRule.model.codeRule.canCombineRightFiled').d('只能合并右侧空字段')
        });
        return;
      }

      var currentField = currentRow[colIndex]; // 重写 field 的 colspan

      if (currentField.colspan && _isNumber(currentField.colspan)) {
        currentField.colspan += 1;
      } else {
        currentField.colspan = 2;
      } // 去除多余的 emptyField


      pComponent.fields[rowIndex] = _concat([], _slice(currentRow, 0, colIndex + 1), _slice(currentRow, colIndex + 2));
      this.handleRefresh();
    }
    /**
     * 取消合并
     */

  }, {
    key: "handleCancelMergeRight",
    value: function handleCancelMergeRight() {
      var _this$props10 = this.props,
          component = _this$props10.component,
          pComponent = _this$props10.pComponent,
          rowIndex = _this$props10.rowIndex,
          colIndex = _this$props10.colIndex;

      if (!(component.colspan && _isNumber(component.colspan))) {
        notification.warning({
          message: intl.get('cpfm.codeRule.model.codeRule.canQCFiled').d('只能拆分合并后的字段')
        });
      }

      if (component.colspan > 2) {
        component.colspan -= 1;
      } else {
        delete component.colspan;
      } // 加上 空字段


      var currentRow = pComponent.fields[rowIndex];
      var sliceIndex = colIndex + 1;
      pComponent.fields[rowIndex] = _concat([], _slice(currentRow, 0, sliceIndex), [_cloneDeep(emptyField)], _slice(currentRow, sliceIndex));
      this.handleRefresh();
    }
    /**
     * 更新 父组件 以刷新视图
     */

  }, {
    key: "handleRefresh",
    value: function handleRefresh() {
      var onRefresh = this.props.onRefresh;

      if (_isFunction(onRefresh)) {
        onRefresh();
      }
    }
  }]);

  return CellControl;
}(React.Component);

export { CellControl as default };